package controller;


import service.Impl.StudentServiceImpl;
import service.StudentService;
import servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/student")
public class StudentController extends BaseServlet {

    private StudentService studentService = new StudentServiceImpl();

    public void doRegister(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        boolean register = studentService.register(request, response);
        String code;
        if(register){
            code = "1";
        }else{
            code = "-1";
        }
        response.getWriter().write(code);
    }

    public void doInitModify(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String stuInfo = studentService.initModify(request, response);
        response.getWriter().write(stuInfo);
    }

    public void doModify(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String code = studentService.modify(request, response);
        response.getWriter().write(code);
    }

    public void doGetStudents(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String students = studentService.getStudents(request, response);
        response.getWriter().write(students);
    }

    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        boolean delete = studentService.delete(request, response);
        String code;
        if(delete) {
            code = "1";
        }else{
            code = "-1";
        }
        response.getWriter().write(code);

    }

    public void isRegister(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        boolean register = studentService.isRegister(username);
        String code;
        if(register){
            code = "1";
        }else{
            code = "-1";
        }
        response.getWriter().write(code);
    }
}